<?php
declare (strict_types=1);

namespace app\index\controller;

use addons\V4\admin\model\OrderTemporary;

use addons\V4\admin\model\ProductCart;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Cart extends Base
{
    public function index()
    {
        $map = ['uid' => $this->user->id];
        $list = ProductCart::getList($map, false, 'update_time', ['product']);
        View::assign('list', $list);
        return View::fetch();
    }

    public function add(): Response
    {
        $param = $this->request->post();
        if (!$param['num'] || $param['num'] <= 0) {
            return Result::fail('数量错误');
        }
        return Result::fastReturn(ProductCart::add($param,$this->user));

    }

    public function edit(): Response
    {
        return Result::fastReturn(ProductCart::edit($this->request->post(), $this->user->id));
    }


    public function submit(): Response
    {
        $param = $this->request->post();
        $param['type'] = 2;
        return Result::fastReturn(OrderTemporary::setTemporary($param, $this->user));
    }
}


